﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using QuanCafe.BusinessLayer;
using QuanCafe.DataLayer;

namespace QuanCafe.Admin
{
    public partial class Category : System.Web.UI.Page
    {
        #region Declare
        string mstrActivity = "";
        DataTable dtList = null;
        SQLHelper db = new SQLHelper();
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.fnLoadData();
            }
        }

        //Load Data
        public void fnLoadData()
        {
            try
            {
                QuanCafe.BusinessLayer.Category Category = new QuanCafe.BusinessLayer.Category();
                DataTable dtb = Category.GetAll();
                dtgCategory.DataSource = dtb;
                dtgCategory.DataBind();
                for (int i = 0; i < dtgCategory.Items.Count ; i++)
                {
                    if (dtb.Rows[i]["Status"].ToString() == "1")
                    {
                        ((ImageButton)dtgCategory.Items[i].FindControl("btnActive")).ImageUrl = "~/Images/icons/btnOK.jpg";

                    }
                    else
                    {
                        ((ImageButton)dtgCategory.Items[i].FindControl("btnActive")).ImageUrl = "~/Images/icons/btnKO.jpg";
                    }
                }
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }

        }
        public void fnClearControl()
        {
            txtCategoryID.Text = "";
            txtCategoryName.Text = "";
            txtMetaDesc.Text = "";
            lblError.Text = "";
        }
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            fnClearControl();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                QuanCafe.BusinessLayer.Category categoryObj = new QuanCafe.BusinessLayer.Category();
                if (txtCategoryID.Text == "")
                {
                    categoryObj.CategoryName = txtCategoryName.Text.ToString();
                    if (categoryObj.CheckCategory() == true)
                    {
                        lblError.Text = "Tên chủ đề này đã tồn tại!Vui lòng nhập lại";
                    }
                    else
                    {
                        categoryObj.activity = "Insert";
                        categoryObj.CategoryName = txtCategoryName.Text.Trim();
                        categoryObj.MetaDesc = txtMetaDesc.Text.Trim();
                        categoryObj.Creator = Session["AdminName"].ToString();
                        categoryObj.InsertUpdate();
                        fnLoadData();
                        lblError.Text = "Thêm mới thành công";
                        fnClearControl();
                    }
                }
                else
                {
                    categoryObj.activity = "Update";
                    categoryObj.CategoryID = Convert.ToInt32(txtCategoryID.Text.Trim());
                    categoryObj.CategoryName = txtCategoryName.Text.Trim();
                    categoryObj.MetaDesc = txtMetaDesc.Text.Trim();
                    categoryObj.Creator = Session["AdminName"].ToString();
                    categoryObj.InsertUpdate();
                    fnLoadData();
                    lblError.Text = "Cập nhập thành công";
                    fnClearControl();
                }
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                string strID = "";
                for (int i = 0; i < dtgCategory.Items.Count; i++)
                {
                    if (((CheckBox)dtgCategory.Items[i].FindControl("chkSelect")).Checked == true)
                    {
                        strID += dtgCategory.Items[i].Cells[0].Text.Trim() + "$";//Cot dau tien phai de cai ID                         
                    }
                }
                CLSCommon.DeleteListRecord("SP_Category", strID, "CategoryID");
                fnLoadData();
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }
        public void fnLoadDataByID(int IntID)
        {
            try
            {
                QuanCafe.BusinessLayer.Category Category = new QuanCafe.BusinessLayer.Category();
                Category.CategoryID = IntID;
                DataRow dr = Category.GetByID();
                txtCategoryID.Text = IntID.ToString();
                txtCategoryName.Text = dr["CategoryName"].ToString();
                txtMetaDesc.Text = dr["MetaDesc"].ToString();

            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }
        private void fnChangeActive(int ID)
        {
            QuanCafe.BusinessLayer.Category Category = new QuanCafe.BusinessLayer.Category();
            Category.CategoryID = ID;
            Category.ChangeActive();
            fnLoadData();
        }

        protected void dtgCategory_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string nName = e.CommandName.ToLower();
            int ID = -1;
            switch (nName)
            {
                case "edit":
                    ID = Convert.ToInt32(dtgCategory.Items[e.Item.ItemIndex].Cells[0].Text);
                    fnLoadDataByID(ID);
                    break;
                case "activechange":
                    ID = Convert.ToInt32(dtgCategory.Items[e.Item.ItemIndex].Cells[0].Text);
                    fnChangeActive(ID);
                    break;
            }
            this.fnLoadData();
        }

        protected void dtgCategory_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            dtgCategory.CurrentPageIndex = e.NewPageIndex;
            this.fnLoadData();
        }

        
    }
}
